package com.zhuiyun.project.api.sysmenu.service.impl;

import com.zhuiyun.project.api.sysmenu.mapper.sysUserPowerMappper;
import com.zhuiyun.project.api.sysuser.entiy.SysUser;
import com.zhuiyun.project.api.sysmenu.entity.SysMenuEntity;
import com.zhuiyun.project.api.sysmenu.mapper.SysMenuMapper;
import com.zhuiyun.project.api.sysmenu.model.SysMenuModel;
import com.zhuiyun.project.api.sysmenu.service.SysMenuService;
import com.zhuiyun.project.common.errorCode.CommonException;
import com.zhuiyun.project.common.errorCode.EmErrorCode;
import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.util.BeanUtils;
import com.zhuiyun.project.util.ObjectUtils;
import com.zhuiyun.project.util.UserUtils;
import com.zhuiyun.project.util.validator.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName SysMenuServiceImpl
 * @Description TODO
 * @Author lzy
 * @Date 2023/5/20 9:53
 **/
@Service
public class SysMenuServiceImpl implements SysMenuService {

    @Autowired
    SysMenuMapper sysMenuMapper;

    @Autowired
    sysUserPowerMappper sysUserPowerMappper;
    /**
    * @Description 获取菜单列表
    * @Author lzy
    * @Date 2023/5/20 10:00
    * @Return com.hb.lottery.common.response.CommonResult
    */
    @Override
    public CommonResult getMenuList() throws CommonException {
        ArrayList<SysMenuEntity> menuList = new ArrayList<>();
        List<SysMenuEntity> sysMenuEntities = sysMenuMapper.selectAll();
        for (SysMenuEntity sysMenuEntity : sysMenuEntities) {
            if (sysMenuEntity.getPid() == 0) {
                SysMenuEntity child = getChild(sysMenuEntity, sysMenuEntities);
                menuList.add(child);
            }
        }
        return CommonResult.ok(menuList);
    }




    /**
    * @Description 菜单管理的菜单删除方法
    * @Author lzy
    * @Date 2023/6/5 15:53
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param id
    */
    @Override
    public CommonResult deleteMenu(Integer id) throws CommonException {

        if ( ObjectUtils.isNotEmpty(id)) {
            int i = sysMenuMapper.deleteByPrimaryKey(id);
            int i1 = sysUserPowerMappper.deletesy(id);
            if (i > 0) {
                return CommonResult.ok(EmErrorCode.DELETE_OK);
            } else {
                return CommonResult.error(EmErrorCode.DELETE_ERROR);
            }
        } else {
            return CommonResult.error(EmErrorCode.DELETE_ERROR);
        }
    }

    /**
    * @Description 菜单管理的菜单修改方法
    * @Author lzy
    * @Date 2023/6/5 15:53
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param sysMenuModel
    */
    @Override
    public CommonResult updateMenu(SysMenuModel sysMenuModel) throws CommonException {
        if (ValidatorUtil.ValidationObject(sysMenuModel)){
            SysMenuEntity sysMenuEntity = new SysMenuEntity();
            BeanUtils.copy(sysMenuModel,sysMenuEntity);
            sysMenuEntity.setUpdateTime(new Date());
            int insert = sysMenuMapper.updateByPrimaryKeySelective(sysMenuEntity);
            if (insert > 0) {
                return CommonResult.ok(EmErrorCode.UPDATE_OK);
            } else {
                return CommonResult.error(EmErrorCode.UPDATE_ERROR);
            }
        } else {
            return CommonResult.error(EmErrorCode.UPDATE_ERROR);
        }
    }

    /**
    * @Description 菜单管理的菜单新增方法
    * @Author lzy
    * @Date 2023/6/5 16:59
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param sysMenuModel
    */
    @Override
    public CommonResult saveMenu(SysMenuModel sysMenuModel) throws CommonException {
        if (ValidatorUtil.ValidationObject(sysMenuModel)){
            SysMenuEntity sysMenuEntity = new SysMenuEntity();
            BeanUtils.copy(sysMenuModel,sysMenuEntity);
            sysMenuEntity.setCreateTime(new Date());
            sysMenuEntity.setUpdateTime(new Date());
            int insert = sysMenuMapper.insert(sysMenuEntity);
            if (insert > 0) {
                return CommonResult.ok(EmErrorCode.SAVE_OK);
            } else {
                return CommonResult.error(EmErrorCode.SAVE_ERROR);
            }
        } else {
            return CommonResult.error(EmErrorCode.SAVE_ERROR);
        }
    }
    
    /**
    * @Description 菜单管理的菜单描述查重方法
    * @Author lzy
    * @Date 2023/6/5 17:00
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param title
    */
    @Override
    public CommonResult checkMenuTitle(String title) throws CommonException {
        SysMenuEntity sysMenuEntity = new SysMenuEntity();
        sysMenuEntity.setTitle(title);
        int i = sysMenuMapper.selectCount(sysMenuEntity);
        if(i > 0){
            return CommonResult.error(i);
        } else {
            return CommonResult.ok(i);
        }
    }

    /**
    * @Description 菜单管理的菜单url路径查重方法
    * @Author lzy
    * @Date 2023/6/5 17:00
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param url
    */
    @Override
    public CommonResult checkMenuUrl(String url) throws CommonException {
        SysMenuEntity sysMenuEntity = new SysMenuEntity();
        sysMenuEntity.setUrl(url);
        int i = sysMenuMapper.selectCount(sysMenuEntity);
        if(i > 0){
            return CommonResult.error(i);
        } else {
            return CommonResult.ok(i);
        }
    }
    
    /**
    * @Description 菜单管理的菜单url路径查重方法
    * @Author lzy
    * @Date 2023/6/5 17:00
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param name
    */
    @Override
    public CommonResult checkMenuName(String name) throws CommonException {
        SysMenuEntity sysMenuEntity = new SysMenuEntity();
        sysMenuEntity.setName(name);
        int i = sysMenuMapper.selectCount(sysMenuEntity);
        if(i > 0){
            return CommonResult.error(i);
        } else {
            return CommonResult.ok(i);
        }
    }

    /**
    * @Description 菜单管理的菜单url路径查重方法
    * @Author lzy
    * @Date 2023/6/5 17:00
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param curl
    */
    @Override
    public CommonResult checkMenuCurl(String curl) throws CommonException {
        SysMenuEntity sysMenuEntity = new SysMenuEntity();
        sysMenuEntity.setCurl(curl);
        int i = sysMenuMapper.selectCount(sysMenuEntity);
        if(i > 0){
            return CommonResult.error(i);
        } else {
            return CommonResult.ok(i);
        }
    }

    @Override
    public CommonResult getMenuByUser() {
        SysUser user = (SysUser)UserUtils.getUser();
        System.out.println(user);
        List<SysMenuEntity> menuByUser = sysMenuMapper.getMenuByUser(user.getId());
        for (SysMenuEntity menu : menuByUser) {
            if (menu.getPid()==0){//判断是否最顶层类型
                updata(menu,menuByUser);//调用方法查找子类型
            }
        }
        for (int i = menuByUser.size()-1;i>=0;i--){
            if (menuByUser.get(i).getPid()!=0){
                menuByUser.remove(i);
            }
        }
        return CommonResult.ok(menuByUser);
    }

    private SysMenuEntity getChild(SysMenuEntity SysMenuEntity, List<SysMenuEntity> SysMenuEntityList) throws CommonException {
        ArrayList<SysMenuEntity> newMenu = new ArrayList<>();
        for (SysMenuEntity entity : SysMenuEntityList) {
            if(SysMenuEntity.getId().equals(entity.getPid())){
                newMenu.add(getChild(entity,SysMenuEntityList));
            }
        }
        SysMenuEntity.setChildren(newMenu);
        return SysMenuEntity;
    }

    /**
     * @description:菜单递归方法
     * @author: dongdong
     * @date: 2023/4/4 13:34
     * @param: [carType, carTypes]
     * @return: void
     **/
    private static void updata(SysMenuEntity carType, List<SysMenuEntity> carTypes) {
        if (carType.getChildren()==null){
            carType.setChildren(new ArrayList<>());
        }
        for (SysMenuEntity type : carTypes) {//循环车类型集合查找当前类型子类型
            if (type.getPid().equals(carType.getId())){
                List<SysMenuEntity> chiledren = carType.getChildren();
                chiledren.add(type);
                carType.setChildren(chiledren);
            }
        }
        if (carType.getChildren().size()!=0){//判断是否拥有子类型
            List<SysMenuEntity> chiledren = carType.getChildren();
            for (SysMenuEntity type : chiledren) {//循环子类型调用方法查找子类型的子类型
                updata(type,carTypes);
            }
            carType.setChildren(chiledren);
        }
    }

}
